package com.example.basic.web.service.newlyManage.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.web.mapper.device.DeviceMapper;
import com.example.basic.web.mapper.newly.SysAdmireMapper;
import com.example.basic.web.mapper.newly.SysPdaFlowMapper;
import com.example.basic.web.mapper.remains.RemainsInfoMapper;
import com.example.basic.web.model.device.Device;
import com.example.basic.web.model.newly.SysPdaFlow;
import com.example.basic.web.model.remains.RemainsInfo;
import com.example.basic.web.response.SysFlowResponse;
import com.example.basic.web.service.newlyManage.ISysFlowManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysFlowManageServiceImpl implements ISysFlowManageService {

    @Autowired
    private SysPdaFlowMapper sysPdaFlowMapper;

    @Autowired
    private RemainsInfoMapper remainsInfoMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public IPage<SysFlowResponse> queryList(Page<SysPdaFlow> queryPage, Integer status) {
        LambdaQueryWrapper<SysPdaFlow> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(status)) {
            if (status == 1) {
                queryWrapper.eq(SysPdaFlow::getFireStatus, 2);
            } else if (status == 0) {
                queryWrapper.ne(SysPdaFlow::getFireStatus, 2);
            }
        }
        queryWrapper.orderByDesc(SysPdaFlow::getCreateAt);
        IPage<SysPdaFlow> sysPdaFlowIPage = sysPdaFlowMapper.selectPage(queryPage, queryWrapper);
        List<SysPdaFlow> records = sysPdaFlowIPage.getRecords();

        List<SysFlowResponse> sysFlowResponseList = records.stream().map(sysPdaFlow -> {
            SysFlowResponse response = BeanUtil.toBean(sysPdaFlow, SysFlowResponse.class);
            RemainsInfo remainsInfo = remainsInfoMapper.selectById(sysPdaFlow.getRemainsId());
            response.setName(remainsInfo.getName());
            response.setSexCode(remainsInfo.getSexCode());
            response.setAge(remainsInfo.getAge());

            if (ObjectUtils.isNotEmpty(sysPdaFlow.getPositionId())) {
                Device device = deviceMapper.selectById(sysPdaFlow.getPositionId());
                response.setNowPositionName(device.getDeviceName());
            }
            return response;
        }).collect(Collectors.toList());

        Page<SysFlowResponse> sysFlowResponsePage = new Page<>(sysPdaFlowIPage.getCurrent(), sysPdaFlowIPage.getSize(), sysPdaFlowIPage.getTotal());
        sysFlowResponsePage.setRecords(sysFlowResponseList);
        return sysFlowResponsePage;

    }
}
